package org.jeecg.modules.sistersophie.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.sistersophie.constans.GlobalConstans;
import org.jeecg.modules.sistersophie.entity.CourseStudentInfo;
import org.jeecg.modules.sistersophie.facade.impl.CourseStudentFacadeService;
import org.jeecg.modules.sistersophie.model.dto.CourseStudentDTO;
import org.jeecg.modules.sistersophie.model.request.CourseAddStudentRequest;
import org.jeecg.modules.sistersophie.model.vo.CourseStudentSearchVO;
import org.jeecg.modules.sistersophie.service.ICourseStudentInfoService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: course_student_info
 * @Author: jeecg-boot
 * @Date:   2021-06-04
 * @Version: V1.0
 */
@Api(tags="course_student_info")
@RestController
@RequestMapping("/sistersophie/courseStudentInfo")
@Slf4j
public class CourseStudentInfoController extends JeecgController<CourseStudentInfo, ICourseStudentInfoService> {

	@Autowired
	private ICourseStudentInfoService courseStudentInfoService;

	@Autowired
	private CourseStudentFacadeService facadeService;
	/**
	 * 分页列表查询
	 *
	 * @param courseStudentInfo
	 * @param req
	 * @return
	 */
	/*@AutoLog(value = "course_student_info-分页列表查询")
	@ApiOperation(value="course_student_info-分页列表查询", notes="course_student_info-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(CourseStudentInfo courseStudentInfo,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<CourseStudentInfo> queryWrapper = QueryGenerator.initQueryWrapper(courseStudentInfo, req.getParameterMap());
		Page<CourseStudentInfo> page = new Page<CourseStudentInfo>(pageNo, pageSize);
		IPage<CourseStudentInfo> pageList = courseStudentInfoService.page(page, queryWrapper);
		return Result.OK(pageList);
	}*/

	@GetMapping(value = "/invoice/list")
	public Result<?> queryInvoiceList(CourseStudentSearchVO courseStudentInfo,
								   HttpServletRequest req) {
		/*if(courseStudentInfo.getPayStatus() == null){
			courseStudentInfo.setPayStatus(3);
		}*/
		if(StringUtils.isNotBlank(courseStudentInfo.getFirstName())){
			courseStudentInfo.setFirstName("%" + courseStudentInfo.getFirstName()+"%");
		}
		if(StringUtils.isNotBlank(courseStudentInfo.getLastName())){
			courseStudentInfo.setLastName("%" + courseStudentInfo.getLastName()+"%");
		}
		List<CourseStudentDTO> list = facadeService.selectInvoiceCourseStudent(courseStudentInfo);
		if(list != null){
			for(CourseStudentDTO dto : list){
				dto.setIsSelect(0);
				if(courseStudentInfo.getInvoiceId() != null && dto.getInvoiceId() != null){
					if(courseStudentInfo.getInvoiceId().intValue() == dto.getInvoiceId().intValue()){
						dto.setIsSelect(1);
					}
				}
				if(dto.getPrice() != null){
					dto.setPriceFormat(dto.getCurrency() + NumberUtil.decimalFormat(GlobalConstans.DECIMARPATTERN,dto.getPrice()));
				}
			}
		}
		return Result.OK(list);
	}

	@AutoLog(value = "course_student_info-分页列表查询")
	@ApiOperation(value="course_student_info-分页列表查询", notes="course_student_info-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(CourseStudentSearchVO courseStudentInfo,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		Page<CourseStudentInfo> page = new Page<CourseStudentInfo>(pageNo, pageSize);
		IPage<CourseStudentDTO> pageList = courseStudentInfoService.selectCourseStudent(page, courseStudentInfo);
		return Result.OK(pageList);
	}


	 @AutoLog(value = "course_student_info-添加")
	 @ApiOperation(value="course_student_info-添加", notes="course_student_info-添加")
	 @PostMapping(value = "/addStudent")
	 public Result<?> addStudent(@RequestBody CourseAddStudentRequest addStudentRequest) {
		 System.out.println(JSONUtil.toJsonStr(addStudentRequest));
		 facadeService.addStudent(addStudentRequest);
		 return Result.OK("Added successfully！");
	 }

	/**
	 *   添加
	 *
	 * @param courseStudentInfo
	 * @return
	 */
	@AutoLog(value = "course_student_info-添加")
	@ApiOperation(value="course_student_info-添加", notes="course_student_info-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody CourseStudentInfo courseStudentInfo) {
		courseStudentInfoService.save(courseStudentInfo);
		return Result.OK("Added successfully！");
	}
	
	/**
	 *  编辑
	 *
	 * @param courseStudentInfo
	 * @return
	 */
	@AutoLog(value = "course_student_info-编辑")
	@ApiOperation(value="course_student_info-编辑", notes="course_student_info-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody CourseStudentInfo courseStudentInfo) {
		courseStudentInfoService.updateById(courseStudentInfo);
		return Result.OK("Edit successfully!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "通过id删除课程学生信息")
	@ApiOperation(value="通过id删除课程学生信息", notes="通过id删除课程学生信息")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		facadeService.deleteCourseStudent(id);
		return Result.OK("Successfully deleted!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "course_student_info-批量删除")
	@ApiOperation(value="course_student_info-批量删除", notes="course_student_info-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.courseStudentInfoService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "course_student_info-通过id查询")
	@ApiOperation(value="course_student_info-通过id查询", notes="course_student_info-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		CourseStudentInfo courseStudentInfo = courseStudentInfoService.getById(id);
		if(courseStudentInfo==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(courseStudentInfo);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param courseStudentInfo
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CourseStudentInfo courseStudentInfo) {
        return super.exportXls(request, courseStudentInfo, CourseStudentInfo.class, "course_student_info");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CourseStudentInfo.class);
    }

}
